package com.nbicc.newscat.controller;

import com.nbicc.common.RequestPageObj;
import com.nbicc.common.ResponseObj;
import com.nbicc.newscat.po.NewsCat;
import com.nbicc.newscat.service.INewsCatService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.Map;

@Api(value = "新闻分类" , description = "控制新闻和目录关系")
@RestController
@RequestMapping(value = "/news/cat")
public class NewsCatController {

    @Resource(name = "NewsCatServiceImpl")
    private INewsCatService newsCatService;

    @ApiOperation(value = "按目录查询新闻" , notes = "参数格式 catId=xxx 是否递归 isRecursion=true")
    @PostMapping("/find")
    @ResponseBody
    public ResponseObj find(@RequestBody RequestPageObj<Map<String,Object>> requestPageObj) throws Exception{
        ResponseObj responseObj = newsCatService.find(requestPageObj);
        return responseObj;
    }

    @ApiOperation(value = "更新新闻目录关系")
    @PutMapping("/updateNewsCat/newsId/{newsId}")
    @ResponseBody
    public ResponseObj updateNewsCat(
            @PathVariable Long newsId,
            @RequestBody NewsCat[] newsCats) throws Exception{
        newsCatService.updateNewsCat(newsId , newsCats);
        return ResponseObj.success();
    }

    @ApiIgnore
    @ApiOperation(value = "统计目录下有多少新闻" , notes = "参数示例 catId=xxx")
    @PostMapping("/countNews")
    @ResponseBody
    public ResponseObj countNews(
            @RequestBody Map<String,Object> parms) throws Exception{
        long newsSiz = newsCatService.countNews(parms);
        return ResponseObj.success().setResult(newsSiz);
    }

}
